<?php

namespace Core\Common;

use App\Controller\Example\BaseExample;

class Common
{
    /**
     * sql对象赋值
     * @return object
     */
    public static function assemble($object,$data){
        foreach ($data as $item => $val){
            if(property_exists($object,$item)){
                $object->$item = $val;
            }
        }
        return $object;
    }
    //翻页通用
    public static function identical($data){
        $assemble = Common::assemble(new BaseExample(), $data);
        if($assemble->page ==null)$assemble->page=1;
        if($assemble->limit ==null)$assemble->limit=50;
        $assemble->pageStart =$assemble->limit*($assemble->page-1);
        return $assemble;
    }
    /**
     * curl请求
     * @return string
     */
    public function sendCurl($url,$params=false,$ispost=0){
        $httpInfo = array();
        $ch = curl_init();
        curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
        curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 3);
        curl_setopt( $ch, CURLOPT_TIMEOUT , 8);
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
        if ($params) {
            if (is_array($params)) {
                $paramsString = http_build_query($params);
            } else {
                $paramsString = $params;
            }
        } else {
            $paramsString = "";
        }
        if( $ispost )
        {
            curl_setopt( $ch , CURLOPT_POST , true );
            curl_setopt( $ch , CURLOPT_POSTFIELDS , $paramsString);
            curl_setopt( $ch , CURLOPT_URL , $url );
        }
        else
        {
            if($paramsString ){
                curl_setopt( $ch , CURLOPT_URL , $url.'?'.$paramsString);
            }else{
                curl_setopt( $ch , CURLOPT_URL , $url);
            }
        }
        $response = curl_exec( $ch );
        if ($response === FALSE) {
            //echo "cURL Error: " . curl_error($ch);
            return false;
        }
        $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
        $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
        curl_close( $ch );
        return $response;
    }
    /**
     * 获取post参数
     * @return array
     */
    public static function getPost(){
        $response = json_decode(file_get_contents('php://input'), true);
        if(empty($response)){
            $response = $_REQUEST;
        }
        $data = [];
        foreach($response as $k=>$v)
        {
            if($k=="token"){
                $data[$k] =$v;
            }else{
                //$data[$k] =Common::filter($v);
                $data[$k] =$v;
            }
        }
        return $data;
    }
    /**
     * 参数过滤
     * @return string
     */
    private static function filter($data){
        $data=str_replace("#","",$data);
        $data=str_replace("<","",$data);
        $data=str_replace(">","",$data);
        //$data=str_replace("/","",$data);
        $data=str_replace("?","",$data);
        $data=str_replace("@","",$data);//邮箱验证使用
        //$data=str_replace(":","",$data);
        $data=str_replace("$","",$data);
        $data=str_replace(";","",$data);
        //$data=str_replace(",","",$data);
        $data=str_replace("&","",$data);
        $data=str_replace("*","",$data);
        //$data=str_replace("'","",$data);
        $data=str_replace("+","",$data);
        //$data=str_replace("-","",$data);
        $data=str_replace("=","",$data);
        //$data=str_replace("\"","",$data);
        $data=str_replace(" ","",$data);	//英文的空格
        $data=str_replace(" ","",$data);	//中文的空格
        $data=str_replace(" ","",$data);	//中文的空格
        //注意，此处不能过滤"|"，因数据库中多个值用"|"做分隔符；
        $data = trim($data);
        $data = stripslashes($data);
        //$data = htmlspecialchars($data);
        return $data;
    }
}